/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025/5/30
 * Time: 17:50
 */

public class Solution {
    public static int i = 0;
    public static TreeNode createTree(int[] nums) {
        int cur = nums[i];
        TreeNode root = null;
        if (cur != -1) {
            root = new TreeNode(cur);
            i++;
            root.left = createTree(nums);
            root.right = createTree(nums);
        }else {
            i++;
        }
        return root;
    }
    public static int ans = 1;
    public static int diameterOfBinaryTree(TreeNode root) {
        depth(root);
        return ans - 1;
    }
    public static int depth(TreeNode node) {
        if (node == null) {
            return 0; // 访问到空节点了，返回0
        }
        int L = depth(node.left); // 左儿子为根的子树的深度
        int R = depth(node.right); // 右儿子为根的子树的深度
        ans = Math.max(ans, L+R+1); // 计算d_node即L+R+1 并更新ans
        return Math.max(L, R) + 1; // 返回该节点为根的子树的深度
    }
    public static void main(String[] args) {
        int ret = diameterOfBinaryTree(createTree(new int[]{1, 2, 4, -1, -1, 5, -1, -1, 3, -1, -1}));
        System.out.println(ret);
    }
}